#include "replacedialog.h"
#include "ui_replacedialog.h"
#include "QMessageBox"
#include <QRegularExpressionMatch>
#include <QRegularExpression>

ReplaceDialog::ReplaceDialog(QWidget *parent,CodeEditor* codeEditor)
    : QDialog(parent)
    , ui(new Ui::ReplaceDialog)
{
    ui->setupUi(this);
    pCodeEditor = codeEditor;
    ui->rbDown->setChecked(true);
}

ReplaceDialog::~ReplaceDialog()
{
    delete ui;
}

void ReplaceDialog::on_btnFindNext_clicked()
{
    QString target = ui->FindTarget->text();//查询的目标字符串

    if(target==""||pCodeEditor==nullptr)
        return;

    QString text = pCodeEditor->toPlainText();//获取文本
    QTextCursor c = pCodeEditor->textCursor();
    int index = -1;


    if (ui->cbRegularExpression->isChecked()) {
        // 使用正则表达式搜索
        QRegularExpression regex(target, ui->cbCaseSensitive->isChecked() ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption);
        QRegularExpressionMatch match;

        if (ui->rbDown->isChecked()) {
            // 向下搜索
            match = regex.match(text, c.position());
            if (match.hasMatch()) {
                index = match.capturedStart();
                c.setPosition(index);
                c.setPosition(index + match.capturedLength(), QTextCursor::KeepAnchor);
                pCodeEditor->setTextCursor(c);
            }
        } else if (ui->rbUp->isChecked()) {
            // 向上搜索
            int startPosition = c.position();
            for (int i = startPosition - 1; i >= 0; --i) {
                match = regex.match(text, i);
                if (match.hasMatch()) {
                    index = match.capturedStart();
                    c.setPosition(index + match.capturedLength());
                    c.setPosition(index, QTextCursor::KeepAnchor);
                    pCodeEditor->setTextCursor(c);
                    break;
                }
            }
        }
    } else {
        // 使用普通字符串搜索
        if (ui->rbDown->isChecked()) {
            index = text.indexOf(target, c.position(), ui->cbCaseSensitive->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive);
            if (index >= 0) {
                c.setPosition(index);
                c.setPosition(index + target.length(), QTextCursor::KeepAnchor);
                pCodeEditor->setTextCursor(c);
            }
        } else if (ui->rbUp->isChecked()) {
            index = text.lastIndexOf(target, c.position() - target.length() - 1, ui->cbCaseSensitive->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive);
            if (index >= 0) {
                c.setPosition(index + target.length());
                c.setPosition(index, QTextCursor::KeepAnchor);
                pCodeEditor->setTextCursor(c);
            }
        }
    }

    if (index < 0) {
        QMessageBox msg(this);
        msg.setWindowTitle("记事本");
        msg.setText(QString("找不到") + target);
        msg.setWindowFlag(Qt::Drawer);
        msg.setIcon(QMessageBox::Information);
        msg.setStandardButtons(QMessageBox::Ok);
        msg.exec();
    }
}


void ReplaceDialog::on_btnChange_clicked()
{
    QString target = ui->FindTarget->text(); // 查询的目标字符串
    QString to = ui->changeTarget->text(); // 替换的字符串

    if (pCodeEditor == nullptr || target.isEmpty() || to.isEmpty()) {
        return;
    }

    QTextCursor cursor = pCodeEditor->textCursor();
    if (cursor.hasSelection()) {
        if (ui->cbRegularExpression->isChecked()) {
            // 使用正则表达式替换
            QRegularExpression regex(target, ui->cbCaseSensitive->isChecked() ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption);
            QRegularExpressionMatch match = regex.match(cursor.selectedText());
            if (match.hasMatch()) {
                cursor.removeSelectedText();
                cursor.insertText(to);
            }
        } else {
            // 使用普通字符串替换
            if (cursor.selectedText() == target) {
                cursor.removeSelectedText();
                cursor.insertText(to);
            }
        }
    }
    on_btnFindNext_clicked(); // 继续查找下一个匹配项
}


void ReplaceDialog::on_btnChangeAll_clicked()
{
    QString target = ui->FindTarget->text(); // 查询的目标字符串
    QString to = ui->changeTarget->text(); // 替换的字符串

    if (pCodeEditor == nullptr || target.isEmpty() || to.isEmpty()) {
        return;
    }

    QString text = pCodeEditor->toPlainText();

    if (ui->cbRegularExpression->isChecked()) {
        // 使用正则表达式全局替换
        QRegularExpression regex(target, ui->cbCaseSensitive->isChecked() ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption);
        text = text.replace(regex, to);
    } else {
        // 使用普通字符串全局替换
        text.replace(target, to, ui->cbCaseSensitive->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive);
    }

    pCodeEditor->clear();
    pCodeEditor->insertPlainText(text);
}


void ReplaceDialog::on_btnCancel_clicked()
{
    accept();
}

